package cn.iocoder.yudao.module.yyzx.service.bed_manage;

import cn.iocoder.yudao.module.yyzx.dal.dataobject.customer.CustomerDO;
import cn.iocoder.yudao.module.yyzx.dal.mysql.bed_manage.Bed_ManageMapper;
import cn.iocoder.yudao.module.yyzx.service.outgoing.mysql.customer.CustomerMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import cn.iocoder.yudao.module.yyzx.controller.admin.bed_manage.vo.*;
import cn.iocoder.yudao.module.yyzx.dal.dataobject.bed.BedDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;



import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.yyzx.enums.ErrorCodeConstants.*;

/**
 * 床位信息 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class BedManageServiceImpl implements Bed_ManageService {

    @Resource
    private Bed_ManageMapper bedManageMapper;
    @Resource
    private CustomerMapper customerMapper;
    @Override
    public Integer createBed(BedRespVO createReqVO) {
        // 插入
        BedDO bed = BeanUtils.toBean(createReqVO, BedDO.class);
        bed.setName(createReqVO.getName()+"号床");
        bed.setBedStatus(3);
        bed.setSort(1);
        CustomerDO customerDO = BeanUtils.toBean(createReqVO, CustomerDO.class);
        bedManageMapper.insert(bed);
        customerMapper.insert(customerDO);
        // 返回
        return bed.getBedId();
    }

    @Override
    public void updateBed(BedRespVO bedRespVO) {
        // 校验存在
        validateBedExists(bedRespVO.getBedId());
        // 更新
        BedDO bedDO = new BedDO();
        bedDO.setBedId(bedRespVO.getBedId());
        bedDO.setRoomNumber(bedRespVO.getRoomNumber());
        bedDO.setName(bedRespVO.getName());
        bedManageMapper.updateById(bedDO);
        CustomerDO customerDO = new CustomerDO();
        customerDO.setCustomerId(bedRespVO.getCustomerId());
        customerDO.setCustomerName(bedRespVO.getCustomerName());
        customerDO.setCustomerSex(bedRespVO.getCustomerSex());
        customerDO.setCheckinDate(bedRespVO.getCheckinDate());
        customerDO.setExpirationDate(bedRespVO.getExpirationDate());
        customerDO.setRecordId(bedRespVO.getRecordId());
        customerMapper.updateById(customerDO);
    }

    @Override
    public void deleteBed(Integer bid,Integer cid) {
        // 校验存在
        validateBedExists(bid);
        validateBedExists(cid);
        // 删除
        bedManageMapper.deleteById(bid);
//        customerMapper.deleteById(cid);
    }

    private void validateBedExists(Integer id) {
        if (bedManageMapper.selectById(id) == null) {
            throw exception(BED_NOT_EXISTS);
        }
    }

    @Override
    public BedRespVO getBed(Integer id) {
        return bedManageMapper.selectByRecordId(id);
    }

    @Override
    public PageResult<BedPageReqVO> getBedPage(BedPageReqVO pageReqVO) {
        return bedManageMapper.selectPage(pageReqVO);
    }
}